package com.woniuxy.web.back;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.woniuxy.entity.DishCategory;
import com.woniuxy.entity.Order;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.entity.ResponseState;
import com.woniuxy.service.DishCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.groups.Default;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin(value="http://192.168.6.186:8080",allowCredentials = "true")
@Slf4j
@RestController
@JsonIgnoreProperties("handler")
@RequestMapping("admin/category")
public class CategoryController_b {
    @Autowired
    private DishCategoryService dishCategoryService;

    @GetMapping("pageList")
    public ResponseEntity<List<DishCategory>> list(Integer current,Integer pageSize,DishCategory dishCategory){
        if(current ==null || current == 0){
            current=1;
        }
        if(pageSize == null || pageSize == 0){
            pageSize=2;
        }
        //条件筛选
        QueryWrapper<DishCategory> queryWrapper =new QueryWrapper<>();
        if(dishCategory.getDishCategoryName() !=null && dishCategory.getDishCategoryName().trim().length() !=0){
            queryWrapper.like("dish_category_name",dishCategory.getDishCategoryName());
        }
        if (dishCategory.getNavable() !=null){
            queryWrapper.eq("navable","y");
        }
        queryWrapper.eq("status","y");
        Page<DishCategory> page=new Page<>(current,2);
        page=dishCategoryService.page(page,queryWrapper);

        return new ResponseEntity(page);
    }

    @PostMapping("add")
    public ResponseEntity add(@RequestBody DishCategory dishCategory, BindingResult result) {
        if(dishCategoryService.getByName(dishCategory.getDishCategoryName()) !=null){
            return ResponseEntity.FIAL;
        }
        dishCategory.setStatus("y");
        dishCategoryService.save(dishCategory);
        return ResponseEntity.SUCCESS;
    }

    //    @ExceptionHandler(DataAccessException.class)
    public ResponseEntity handerException() {
        log.error("增加种类出错，{}");
        return ResponseEntity.FIAL;
    }
    @GetMapping("get/{id}")
    public ResponseEntity<DishCategory> get(@PathVariable int id){
        return new ResponseEntity<>(dishCategoryService.getById(id));
    }

    /**
     * 删除对应id的分类
     * @param id
     * @return
     */
    @RequestMapping("del/{id}")
    public ResponseEntity del(@PathVariable int id) {
        dishCategoryService.removeById(id);
        return ResponseEntity.SUCCESS;
    }

    @PostMapping ("update")
    public ResponseEntity<?> update(@RequestBody DishCategory dishCategory){

        dishCategory.setStatus("y");
        dishCategoryService.update(dishCategory);
        return ResponseEntity.SUCCESS;
    }

    @PostMapping("list")
    public ResponseEntity<List<DishCategory>> list(){
        List<DishCategory> dishCategoryList = dishCategoryService.getAll();
        return new ResponseEntity<List<DishCategory>>(dishCategoryList);
    }

}
